package cn.dugcrs.org.service.impl;

import cn.dugcrs.basic.service.impl.BaseServiceImpl;
import cn.dugcrs.basic.utils.QueryResult;
import cn.dugcrs.org.domain.Department;
import cn.dugcrs.org.mapper.DepartmentMapper;
import cn.dugcrs.org.query.DepartmentQuery;
import cn.dugcrs.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Description:
* @Author: Gubcog
* @Version: 1.0
* @Date:  2022/4/19 18:39
*/
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        String dirPath = "";
        if (department.getParent() == null){
            dirPath = "/" + department.getId();
        }else {
            Department dept = departmentMapper.loadById(department.getParent().getId());

            if (dept != null){
                dirPath = dept.getDirPath() + "/" + department.getId();
            }
        }

        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        String dirPath = "";
        if (department.getParent() == null){
            dirPath = "/" + department.getId();
        }else {
            Department dept = departmentMapper.loadById(department.getParent().getId());

            if (dept != null){
                dirPath = dept.getDirPath() + "/" + department.getId();
            }
        }

        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }



    /**
    * @Title: findDepartmentTree
    * @Description: 查询部门树数据
    * @Author: Gubcog
    * @Version: 1.0
    * @Date:  2022/4/24 18:26
    * @Parameters: * @param
    * @Return java.util.List<cn.dugcrs.org.domain.Department>
    */
    @Override
    public List<Department> findDepartmentTree() {
        List<Department> depts = departmentMapper.loadAll();

        System.out.println(depts);

        // 创建一个空的List，用于装遍历出来的一级对象
        List<Department> trees = new ArrayList<>();

        // 把list集合数据转换为map
        Map<Long, Department> deptMap = new HashMap<>();
        for (Department dept : depts) {
            deptMap.put(dept.getId(), dept);
        }

        for (Department dept : depts) {
            // 查找到上级Id为空的对象，就是一级对象
            if (dept.getParent_id() == null){
                // 找到一级，放入到空集合中
                trees.add(dept);
            }else { // 如果父级Id不为null，那么意味着他是其它级数据
                // 得到父亲
                Department department = deptMap.get(dept.getParent_id());
                if (department != null){
                    // 将儿子放入到父亲中
                    department.getChildren().add(dept);
                }
            }
        }
        System.out.println(trees);
        return trees;
    }


}
